import store from '../store/index.js'
import router from '../router/index.js'

const moduleContext = require.context('@/views', true, /\.vue$/)

// 动态导入模块什么缺点?

// 根据返回权限创建 用户对应的异步路由
export const dynamicRouter = async () => {
  try {
    await store.dispatch('userAuth/getAuthMenus')

    let authMenus = store.state.userAuth.authMenus

    // 用户菜单权限为空 跳转到登录页面
    if (!authMenus.length) {
      return Promise.reject(new Error('请先登录'))
    }

    authMenus.forEach(item => {
      item.children && delete item.children
      if (item.component && typeof item.component == 'string') {
        const componentKey = `.${item.component}.vue`
        item.component = moduleContext(componentKey).default
      }
    })

    // router.addRoutes(item)
  } catch (e) {
    console.log(11231)
  }
}
